package com.sz.zhiling.controller;

import com.sz.zhiling.dao.UserDao;
import com.sz.zhiling.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.persistence.criteria.CriteriaBuilder;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserDao userDao;
    @RequestMapping("/findAll")
    public List<User> findAllUsers(){
        return userDao.findAll();

    }
    //修改and插入
    @RequestMapping("/save")
    public String insertUser(){
        User user=new User(5,"jonag","12323");
        userDao.save(user);
        return "success";
    }
    @RequestMapping("/findbyId")
    public List<User> findById(String name){
        List<Integer> list=new ArrayList();
        list.add(5);
        list.add(6);
        return userDao.findAllById(list);
    }
    @RequestMapping("/findByNameLike")
    public List<User> findUserByNameLike(){
        User user=new User();
        user.setUsername("j");
        ExampleMatcher exampleMatcher=ExampleMatcher.matching()
                .withMatcher("username",ExampleMatcher.GenericPropertyMatchers.contains());
        Example example= Example.of(user,exampleMatcher);
        return userDao.findAll(example);
    }
    @RequestMapping("/deleteById")
    public String deleteByUserid(){
        userDao.deleteById(5);
        return "success";
    }
}
